var timestamp;
var loginManager;
$(function(){
	loginManager = new LoginManager();
	//console.log(loginManager);
});
;(function(){
	//构造函数
	LoginManager = function(){
		this.settings = $.extend(true,{},LoginManager.defaults);
		this.init();
	};
	$.extend(LoginManager,{
		defaults:{
			USER_CODE:'#username',
			PASSWORD:'#password',
			VERIFY_CODE:'#code',
			SWITCH_ID:'#exchange',
			LOGIN_BUTTON:'#signin',
			IMAGE_ID:"#imgObj",
			FORM_ID:'#form'
		},
		//添加方法
		prototype:{
			init:function(){
				var _this = this;
				_this.bindEvents();
				_this.changeImg();
				$(_this.settings.USER_CODE).focus();
			},
			bindEvents:function(){
				var _this = this;
				$(_this.settings.LOGIN_BUTTON).bind("click",function(){
					_this.logon();
				});
				$(_this.settings.SWITCH_ID).bind("click",function(){
					_this.changeImg();
				});
				$(_this.settings.IMAGE_ID).bind("click",function(){
					_this.changeImg();
				});
				$(_this.settings.USER_CODE).bind("keydown",function(event){
					if(event.keyCode==13) {//回车
						_this.logon();
					}else{
						var userCode = $(_this.settings.USER_CODE).val();
						if(userCode.length>20){
							_this.showError(_this.settings.USER_CODE,"用户名不能超过20位");
							$(_this.settings.USER_CODE).val('');
						}
					}
					
				});
				$(_this.settings.PASSWORD).bind("keydown",function(event){
					if(event.keyCode==13) {//回车
						_this.logon();
					}
				});
				$(_this.settings.VERIFY_CODE).bind("keydown",function(event){
					if(event.keyCode==13) {//回车
						_this.logon();
					}
				});
			},
			logon:function(){
				var _this = this;
				_this.validate();
				var err = $('#errorTxt').html();
				if(err!="")
					return false;
				var username = $(_this.settings.USER_CODE).val();
				var pwd = $(_this.settings.PASSWORD).val();
				var code = $(_this.settings.VERIFY_CODE).val();
				var url="../login/logining?timestamp="+timestamp;
				$.ajax({
					async: false,
					type:"POST", 
					url:url,
					data:{"username":username,"password":pwd,"code":code},
					dataType:'json',
					success:function(data){
						if(data.RES_RESULT=="SUCCESS"){
							//登陆成功
							window.location.href = "../productManage/goToPage?pagePath=hot-product";
						}else{
							var errMsg = data.RES_MSG;
							if(errMsg=="validateCodeError")
								_this.showError(_this.settings.VERIFY_CODE,"验证码不正确");
							else
								_this.showError(_this.settings.USER_CODE,"用户名或密码不正确");
							_this.changeImg();
						}
					},
					error:function (XMLHttpRequest, textStatus, errorThrown) {
					   var errmsg = XMLHttpRequest.responseJSON.message;
					   _this.showError(_this.settings.USER_CODE,"用户名或密码不正确");
						_this.changeImg();
					}
				});
			},
			changeImg:function (){  
				var _this = this;
			    var imgSrc = $(_this.settings.IMAGE_ID);     
			    var src = imgSrc.attr("src"); 
			    imgSrc.attr("src",_this.chgUrl(src));
			},
			//为了使每次生成图片不一致，即不让浏览器读缓存，所以需要加上时间戳     
			chgUrl:function(url){     
			    timestamp = (new Date()).valueOf(); 
			    var urlurl = url.substring(0, url.indexOf("getImage")) + "getImage";
			    urlurl = urlurl + "?timestamp=" + timestamp;     
			    return urlurl;     
			},
			validate:function(){
				var _this = this;
				var username = $(_this.settings.USER_CODE).val();
				var pwd = $(_this.settings.PASSWORD).val();
				var code = $(_this.settings.VERIFY_CODE).val();
				$('[name="warn-username"]').addClass('undis');
				$(_this.settings.USER_CODE).removeClass('focus');
				$(_this.settings.PASSWORD).removeClass('focus');
				$(_this.settings.VERIFY_CODE).removeClass('focus');
				$('#errorTxt').html("");
				if(username==""){
					_this.showError(_this.settings.USER_CODE,"用户名不能为空");
				}else if(pwd==""){
					_this.showError(_this.settings.PASSWORD,"密码不能为空");
				}else if(code==""){
					_this.showError(_this.settings.VERIFY_CODE,"验证码不能为空");
				}
			},
			showError:function(elementId,errorMsg){
				$('[name="warn-username"]').removeClass('undis');
				$('#errorTxt').html(errorMsg);
				$(elementId).addClass('focus');
				$(elementId).focus();
			}
		}
	});
})(jQuery);